from django import forms
import asc_db.models as models
import asc_db.widgets as widgets

#------------------------------------------------------------------------------


class GroupOrTeamForm(forms.ModelForm):
    name = forms.CharField(max_length=255)
    description = forms.Textarea()

    class Meta:
        model = models.GroupOrTeam

#------------------------------------------------------------------------------


class AddressForm(forms.ModelForm):
    flat_number = forms.CharField(max_length=255, required=False)
    house_name = forms.CharField(max_length=255, required=False)
    street_number = forms.CharField(max_length=255, required=False)
    street = forms.CharField(max_length=255)
    district = forms.CharField(max_length=255, required=False)
    city = forms.CharField(max_length=255)
    state_or_province = forms.CharField(max_length=255, required=False)
    postal_code = forms.CharField(max_length=7)
    country = forms.CharField(max_length=255)
    phone = forms.CharField(max_length=11, required=False)
    notes = forms.Textarea()
    in_parish = forms.CheckboxInput()

    # Override the clean method to stop form from saving if we are
    # missing all of flat_number, house_name, street_number
    def clean(self):
        cleaned_data = super(AddressForm, self).clean()
        if cleaned_data.get("flat_number") in ['', None] and \
           cleaned_data.get("house_name") in ['', None] and \
           cleaned_data.get("street_number") in ['', None]:
            raise forms.ValidationError("Need a flat/house number or name.")
        return cleaned_data

    class Meta:
        model = models.Address

#------------------------------------------------------------------------------


class PersonForm(forms.ModelForm):
    title = forms.ModelChoiceField(queryset=models.Title.objects.all())
    first_name = forms.CharField(max_length=255)
    middle_names = forms.CharField(max_length=255, required=False)
    last_name = forms.CharField(max_length=255)
    known_as = forms.CharField(max_length=255, required=False)
    sex = forms.ChoiceField(choices=models.SEXES, widget=forms.RadioSelect())
    date_of_birth = forms.DateField(widget=forms.DateInput(
                                        attrs={"class": "datepicker"}))
    address = forms.ModelChoiceField(queryset=models.Address.objects.all(),
                                     widget=widgets.SelectModelWidget())
    personal_email = forms.EmailField(required=False)
    work_email = forms.EmailField(required=False)
    personal_phone = forms.CharField(max_length=11, required=False)
    work_phone = forms.CharField(max_length=11, required=False)
    notes = forms.Textarea()

    # Override the clean method to stop form saving if the selected title
    # is gender specific and does not match the entered sex
    def clean(self):
        cleaned_data = super(PersonForm, self).clean()
        title = cleaned_data.get("title", None)
        sex = cleaned_data.get("sex", None)
        if title not in ['', None] and \
           sex not in ['', None]:
            if int(title.sex_specific) != int(sex):
                raise forms.ValidationError("Title does not match sex.")
        return cleaned_data

    class Meta:
        model = models.Person
        exclude = ('groups', 'relations',)
